package com.xuecheng.content.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.commons.constants.CourseConstants;
import com.xuecheng.commons.enums.ErrorCode;
import com.xuecheng.commons.model.dto.CourseTeacherDto;
import com.xuecheng.commons.model.vo.CourseTeacherVo;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.utils.AuthInfoHolder;
import com.xuecheng.commons.utils.BeanHelper;
import com.xuecheng.content.config.MinioTemplate;
import com.xuecheng.content.domain.CourseBase;
import com.xuecheng.content.domain.CourseTeacher;
import com.xuecheng.content.mapper.CourseTeacherMapper;
import com.xuecheng.content.service.CourseBaseService;
import com.xuecheng.content.service.CourseTeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.web.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 课程-教师关系表 服务实现类
 * </p>
 *
 * @author itheima
 * @since 2022-09-14
 */
@Service
public class CourseTeacherServiceImpl extends ServiceImpl<CourseTeacherMapper, CourseTeacher> implements CourseTeacherService {

    @Autowired
    private CourseBaseService courseBaseService;
    @Autowired
    private MinioTemplate minioTemplate;

    @Override
    public ResponseResult getTeacherListById(Long courseId) {
        //1.构建查询语句 按课程id查询教师列表
        LambdaQueryWrapper<CourseTeacher> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CourseTeacher::getCourseId, courseId);
        List<CourseTeacher> courseTeacherList = this.list(queryWrapper);
        List<CourseTeacherVo> courseTeacherListVo = new ArrayList<>();
        for (CourseTeacher courseTeacher : courseTeacherList) {
            CourseTeacherVo courseTeacherVo = BeanHelper.copyProperties(courseTeacher, CourseTeacherVo.class);
            courseTeacherVo.setCourseTeacherId(courseTeacher.getId());
            courseTeacherListVo.add(courseTeacherVo);
        }
        return ResponseResult.okResult(courseTeacherListVo);
    }

    @Override
    public ResponseResult saveCourseTeacher(CourseTeacherDto courseTeacherDto) {
        //1.DTO->entity，并补全信息
        CourseTeacher courseTeacher = BeanHelper.copyProperties(courseTeacherDto, CourseTeacher.class);
        //ID信息，无法复制手动添加
        courseTeacher.setId(courseTeacherDto.getCourseTeacherId());
        //创建时间
        if (courseTeacherDto.getCourseTeacherId() == null) {
            courseTeacher.setCreateDate(new Date());
        }
        //2.参数判空
        if (!StrUtil.isAllNotBlank(courseTeacher.getTeacherName()
                , courseTeacher.getCourseId().toString(),
                courseTeacher.getIntroduction(),
                courseTeacher.getPhotograph(),
                courseTeacher.getPosition())) {
            throw new BusinessException(ErrorCode.PARAMSERROR);
        }
        //2.获取课程信息，对状态进行判断是否可修改
        CourseBase courseBase = courseBaseService.getById(courseTeacher.getCourseId());
        //已提交 已审核 已发布 不允许修改
        String auditStatus = courseBase.getAuditStatus();
        if (StrUtil.equals(auditStatus, CourseConstants.AUDIT_COMMIT_STATUS) ||
                StrUtil.equals(auditStatus, CourseConstants.AUDIT_PASTED_STATUS) ||
                StrUtil.equals(auditStatus, CourseConstants.AUDIT_PUBLISHED_STATUS)) {
            throw new BusinessException(ErrorCode.UPDATEEERROR);
        }
        courseTeacher.setCompanyId(AuthInfoHolder.getCompanyId());
        //3.保存或更新
        this.saveOrUpdate(courseTeacher);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult deleteTeacher(Long courseId, Long teacherId) {
        //0.获取教师信息
        CourseTeacher courseTeacher = this.getById(teacherId);
        //1.删除教师信息
        this.removeById(teacherId);
        //2.删除教师头像
        String str = courseTeacher.getPhotograph();
        String fileName = str.substring(str.lastIndexOf("/") + 1);
        String format = new SimpleDateFormat("yyyy/MM/dd/").format(courseTeacher.getCreateDate());
        String finName = "/images/" + format + fileName;
        minioTemplate.delete(finName);
        return ResponseResult.okResult();
    }
}
